#include<stdio.h>
#include<stdlib.h>
#include"SeqStack.h"
LinkStack InitStack(LinkStack Stack)
{
    Stack = (LinkStack)malloc(sizeof(LStackNode));
    if(Stack == NULL)
    {
        printf("INIT ERROR");
        exit(-1);
    }
    return Stack;
}

int StackEmpty(LinkStack Stack)
{
    if(Stack == NULL)
    {
        printf("STACK ERROR");
        exit(-1);
    }
    else
    if(Stack->next == NULL) return 1;
    else return 0;
}

int PushStack(LinkStack Stack, DataType e)
{
    if(Stack == NULL)
    {
        printf("STACK ERROR");
        exit(-1);
    }
    else
    {
        LinkStack p = (LinkStack)malloc(sizeof(LStackNode));
        p->next = Stack->next;
        p->data = e;
        Stack->next = p;
        return 1;
    }
}

int PopStack(LinkStack Stack, DataType *e)
{
    if(Stack == NULL)
    {
        printf("STACK ERROR");
        exit(-1);
    }
    else
    if(Stack->next == NULL)
    {
        printf("STACK IS EMPTY");
        exit(-1);
    }
    else
    {
        LinkStack p = Stack->next;
        Stack->next = p->next;
        *e = p->data;
        free(p);
        return 1;
    }
}

int GetStackTop(LinkStack Stack, DataType *e)
{
    if(Stack == NULL)
    {
        printf("STACK ERROR");
        exit(-1);
    }
    else 
    if(Stack->next == NULL)
    {
        printf("STACK IS EMPTY");
        exit(-1);
    }
    else
    {
        *e = Stack->next->data;
        return 1;
    }
}

int StackLength(LinkStack Stack)
{
    if(Stack == NULL)
    {
        printf("STACK ERROR");
        exit(-1);
    }
    else
    {
        int i = 0;
        while(Stack->next)
        {
            i++;
            Stack = Stack->next;
        }
        return i;
    }
}

void DestroyStack(LinkStack Stack)
{
    if(Stack == NULL) return;
    while(Stack->next)
    {
        LinkStack p = Stack->next;
        Stack->next = p->next;
        free(p);
    }
}

